package cc.wsyw126.java.nowcoder;

/**
 * Created by Administrator on 2017/4/15.
 * 给定一个二维数组，数组中的元素只有0和1，求面积最大的全1方阵的面积（就是矩阵内包含的全是1）。
 */
public class MatrixMaxMatrixArea {
    public static int getMaxArea(char[][] area) {
        if (area == null || area.length == 0) {
            return 0;
        } else {
            int max = 0;
            int[][] maxArea = new int[area.length][area[0].length];
            if (area[0].length == 0) {
                return 0;
            } else {
                for (int i = 0; i < area.length; i++) {
                    for (int j = 0; j < area[i].length; j++) {
                        if (i == 0 || j == 0) {
                            maxArea[i][j] = area[i][j] - '0';
                            if (maxArea[i][j] > max) {
                                max = maxArea[i][j];
                            }
                        } else if (area[i][j] == '0') {
                            maxArea[i][j] = 0;
                        } else {
                            int length = Math.min(maxArea[i - 1][j - 1], Math.min(maxArea[i - 1][j], maxArea[i][j - 1]));
                            int nowArea = (int) Math.pow(Math.sqrt(length) + 1, 2);
                            maxArea[i][j] = nowArea;
                            if (nowArea > max) {
                                max = nowArea;
                            }
                        }
                    }
                }
                return max;
            }
        }
    }

    public static void main(String[] args) {
        //char[][] test=new char[][]{"110".toCharArray(),"111".toCharArray(),"111".toCharArray()};
        char[][] test = new char[][]{"1".toCharArray()};
        char[][] test1 = new char[][]{{'1','1','1'},{'1','1','1'},{'1','0','1'}};

        System.out.println(getMaxArea(test1));

    }
}
